package cn.wolfcode.web.controller;


import cn.wolfcode.transfer.domain.Activity;
import cn.wolfcode.transfer.qo.JsonResult;
import cn.wolfcode.transfer.service.IActivityService;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;


    @RequiredPermission(name="投票页面",expression="activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }


    @RequiredPermission(name="投票删除",expression="activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="投票新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Long[] optionsRadios,Long id) {
       activityService.poll(optionsRadios,id);
        return new JsonResult();
    }


    @RequiredPermission(name="投票新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/poll")
    public String poll(Long id, Model model) {

            model.addAttribute("activity_item",activityService.get(id));
            model.addAttribute("activity",activityService.getTitle(id));

        return "activity/poll";
    }
    @RequiredPermission(name="投票新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/page")
    public String page(Long id, Model model) {

            model.addAttribute("activity_item",activityService.get(id));
            model.addAttribute("activity",activityService.getTitle(id));

        return "activity/page";
    }
}
